<!DOCTYPE html>
<html>
<head>
    {include file='public/meta'/}


</head>
<body>
{include file='public/header'/}
<!------------------导航-------------------------->
<section id="content">
    {include file='public/left'/}
    <!--------------------------主要内容区域------------------------------------->
    <div class="content">
        <div class="page-heading mgl15 mgr15">
            <ol class="breadcrumb">
                <li><a href="#">充值中心</a></li>
                <li class="active">添加面额</li>
            </ol>
        </div>
        <div class="panel panel-default mgl15 mgr15">
            <form class="form-horizontal" action="" method="post"onsubmit="return check()" >
                <input type="hidden" name="type" value="1">
                <div class="panel-body">

                    <div class="form-group">
                        <label class="col-sm-2 control-label ng-binding">充值额度：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="cz_money" autocomplete="off"
                                   placeholder="" required="required">
                            <div class="r-message text-danger ng-scope ng-inactive"></div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label ng-binding">赠送额度：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="zs_money" autocomplete="off" placeholder="" required="required">
                            <div class="r-message text-danger ng-scope ng-inactive"></div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label ng-binding">每人限购数量：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="limit_sume" autocomplete="off" placeholder="" required="required" value="0">
                            <div ng-if="property.desc" class="r-desc ng-binding ng-scope" ng-bind-html="property.desc | to_trusted">为0时表示不限制</div>
                        </div>
                    </div>
                    <div class="form-group ">
                        <label class="col-xs-12 col-sm-3 col-md-2 control-label "><span class="text-danger">*</span> 选择要赠送的卡券</label>
                        <div class="col-xs-10 col-sm-10 col-md-10 form1" >
                            <div class="gift-record"id="sel1">
                                <select name="card_arr[zs_card_id][]"  class="form-control cardsel"id="opt">
                                    <option value="">--请选择--</option>


                                </select>

                                <input type="number" class="form-control"min="1"placeholder="请填写"value="1"name="card_arr[zs_no][]"data="num"id="num">
                            </div>
                            <a href="javascript:;"  data-counts="1" id="datacount" style="color: #2db7f5;">添加赠送</a>
                        </div>


                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label ng-binding">积分：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="bonus" autocomplete="off" placeholder="" required="required"value="1">
                            <div ng-if="property.desc" class="r-desc ng-binding ng-scope" ng-bind-html="property.desc | to_trusted">充值赠送对应的积分如:100则赠送100积分,可以为0</div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label ng-binding">描述说明：</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="zs_show">
                            <div ng-if="property.desc" class="r-desc ng-binding ng-scope" ng-bind-html="property.desc | to_trusted">充值描述</div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label ng-binding">状态：</label>
                        <div class="col-sm-10">
                            <select name="statu" id="" class="form-control">
                                <!--<option value="">&#45;&#45;请选择&#45;&#45;</option>-->
                                <option value="1">开启</option>
                                <option value="2">关闭</option>
                            </select>
                            <div class="r-message text-danger ng-scope ng-inactive"></div>
                        </div>
                    </div>

                </div>
                <div class="panel-footer">
                    <button class="btn btn-success" type="submit">保存</button>
                </div>
            </form>
        </div>
        {include file='public/footer'/}
    </div>
</section>
<script>
    $(function(){
        $.ajax({
            url: "{:url('cards/getcardlist')}",
            type:"post",
            data: {
            },
            success: function (data) {

                var text =data.data;
                //console.log(text);
                var option2="";
                for(var i=0; i<text.length; i++){
                   // console.log(text[i].card_id);

                    option2 +='<option value="'+text[i].card_id+'" data-title="'+text[i].title+' "data-limit="'+text[i].get_limit+'">'+text[i].title+'  ( 最大可选卡券数量为'+text[i].get_limit+' )</option>\n'

                }
                $('#sel1').find("select").append(option2);
                $("#opt").change(function(){

                    limit=parseInt($("#sel1").find("option:selected").attr("data-limit"));
                    var nu=parseInt($("#num").val());

                    $("#num").attr("max",limit);
                        // console.log(333333,limit);




                });

                $("#datacount").click(function(){


                    datacount=$(this).attr('data-counts');
                    get_limit=parseInt($("#sel"+datacount).find("option:selected").attr("data-limit"));
                    num=parseInt($("#sel"+datacount).find("input").val());
                    val=$("#sel"+datacount).find("option:selected").val();
                    if( $("#sel"+datacount).find("option:selected").val()==""){
                        alert("卡券选项不能为空");
                        return;
                    }
                     else if(num>get_limit){
                        // console.log(num,get_limit);
                        //  console.log(num>get_limit);
                        alert("你选择的卡券数量超过了该卡券的领取数量!");
                        return;
                    }
                    else{

                        datacount2=parseInt(datacount)+1;
                        $(this).attr('data-counts',datacount2);
                        var html1 ='                        <div class="col-xs-12 col-sm-12 col-md-12 addcord" >\n' +
                            '                            <div class="gift-record"id="sel'+datacount2+'">\n' +
                            '                                <select name="card_arr[zs_card_id][]"  class="form-control" >\n' +
                            '                                    <option value="" selected="selected">--请选择--</option>\n' ;

                        var html3= '                                </select>\n' +
                            '                                <input type="number" class="form-control" min="1" placeholder="请填写" value="1" name="card_arr[zs_no][]">\n' +
                            '                            </div>\n' +
                            '                             <a href="javascript:;" onclick="delsel(this)">删除</a>\n' +
                            '                    </div>';

                        $('.form1').append(html1+option2+html3);

                        $("#sel"+datacount2).find("select").change(function(){

                            limit=$("#sel"+datacount2).find("option:selected").attr("data-limit");
                            $("#sel"+datacount2).find("input").attr("max",limit);



                        });

                    }




                })
            }
        })


    })
</script>
<script>
function check(){


    var items = document.getElementsByTagName('select'),
        len = items.length,
        i = 0,
        valMap = {},
        sel,
        val;

    // 遍历并保存各个下拉列表的值于valMap
    for(; i < len; i++){
        sel = items[i];
        val = sel.options[sel.selectedIndex].value;

        // 没有相同的取值时保存该值，否则返回false
        if(!valMap[val]){
            valMap[val] = 1;
        }else{
            alert('请删除重复的卡券！');
            return false;
        }
        if($("#sel1").find("option:selected").val()==""){
            alert("请选择卡券！");
           return false;
        }
    }

    // 校验通过，全部都不相同
    return true;

}

</script>
<script>

    function delsel(obj){
//
        $(obj).parent().remove();


    }

</script>
</body>

</html>
